package br.com.skpharma.db;



import java.sql.*;

public class DB{ 
	Connection con;
	Statement stmt;
	ResultSet rs;
	String url;
	String usr;
	String pass;

	public DB(){
		url = "";
		usr = "";
		pass = "";

	}	

	public DB(String n_url, String n_usr, String n_pass){
		url = n_url;
		usr = n_usr;
		pass = n_pass;		
	}

	public boolean connect(){
		try{
			Class.forName("org.postgresql.Driver");
			con = DriverManager.getConnection(url,usr,pass);
			return(true);
		}
		catch(Exception e){
			return(false);
		}	
	}

	public void disconnect(){
		try{
			con.close();
			stmt.close();
			rs.close();
		}
		catch(Exception e){}			
	}

	public void clear(){
		try{
			stmt.close();
			rs.close();
		}
		catch(Exception e){}			
		
	}
	
	public boolean select(String sql){
		try{
			stmt = con.createStatement();		
			rs = stmt.executeQuery(sql);
			return(true);
		}
		catch(Exception e){
			return(false);
		}	
	}

	public boolean update(String sql){
		try{
			stmt = con.createStatement();		
			int i = stmt.executeUpdate(sql);
			return(true);
		}
		catch(Exception e){
			return(false);
		}	
	}

	public int get_size(){
		try{			
			return(rs.getFetchSize());
		}
		catch(Exception e){
			return(0);
		}
	}

	public boolean moveBack(){
		try{
			return(rs.previous());
		}
		catch(Exception e){
			return(false);
		}			
	}

	public boolean moveNext(){
		try{
			return(rs.next());
		}
		catch(Exception e){
			return(false);
		}			
	}

	public String getString(String col){
		try{
			return(rs.getString(col));
		}
		catch(Exception e){
			return(new String(""));
		}	
	}

	public int getInt(String col){
		try{
			return(rs.getInt(col));
		}
		catch(Exception e){
			return(-999);
		}	
	}

	public float getFloat(String col){
		try{
			return(rs.getFloat(col));
		}
		catch(Exception e){
			return(-999);
		}	
	}

	public Date getDate(String col){
		try{
			return(rs.getDate(col));
		}
		catch(Exception e){
			return(null);
		}	
	}

	/*public static void main(String args[]){
		DB db = new DB("teste","","");
		if(db.connect()){
			db.select("select nome from empregado");
			int j = db.get_size();
			for (int i = 0; i<j; i++){ 
				System.out.println(db.get_String("nome"));
				db.moveNext();
			}
			db.disconnect();
		}		
	}*/
}		